package _03_栈;

import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/implement-queue-using-stacks/
 * @Author: haogege
 * @Date: 2021/9/6
 */
public class _232_用栈实现队列 {

    private Stack<Integer> outStack = new Stack<>();

    private Stack<Integer> inStack = new Stack<>();

    /** Initialize your data structure here. */
    public _232_用栈实现队列() {

    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        // 压入入栈栈
        inStack.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        // 出栈，如果出栈队列不为空，则弹出出栈队列元素，如果为空，则将入栈栈的元素全部放入出栈
       inToOut();
        return outStack.pop();
    }

    /** Get the front element. */
    public int peek() {
        inToOut();
        return outStack.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return inStack.empty() && outStack.empty();
    }

    private void inToOut() {
        if (outStack.empty()) {
            while (!inStack.empty()) {
                outStack.push(inStack.pop());
            }
        }
    }

}
